﻿using Hotel.Model.admin;
using MD5Hash;
using Microsoft.VisualBasic;
using System.ComponentModel.DataAnnotations;
using System.Net.WebSockets;

namespace Hotel.DAL
{
    public class adminDAL
    {

        public adminDAL(HotelConText hotelConText)
        {
            HotelConText = hotelConText;
        }

        public HotelConText HotelConText { get; }










        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public adminModel Login(string UserName)
        {
            return HotelConText.admins.FirstOrDefault(x => x.UserName.Equals(UserName));
        }






        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="adminModel"></param>
        /// <returns></returns>
        public int adminAdd(adminModel adminModel)
        {

          
            HotelConText.admins.Add(adminModel);
            return HotelConText.SaveChanges();

        }




        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="count">总条数</param>
        /// <param name="page">总页数</param>
        /// <param name="UserName"></param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页几条</param>
        /// <returns></returns>
        public List<adminModel> adminList(out int count, out int page, string? UserName = "", int index = 1, int size = 3)
        {

            var list = HotelConText.admins.Where(x => x.IsDeleted == false).AsQueryable();
            if (!string.IsNullOrEmpty(UserName))
            {

                list = list.Where(x => x.UserName.Contains(UserName));
            }

            count=list.Count();
            page = (int)Math.Ceiling(count * 1.0 / size);
            return list.OrderBy(x=>x.id).Skip((index-1)* size).Take(size).ToList();
        }







        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int adminDel(int id) 
        {
            var adminDels = HotelConText.admins.Find(id);
            adminDels.IsDeleted= true;
            return HotelConText.SaveChanges();
        }

       





        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public adminModel adminAll(int id) 
        {
        
            return HotelConText.admins.Find(id);


        }




        /// <summary>
        /// 修改·
        /// </summary>
        /// <param name="adminModel"></param>
        /// <returns></returns>
        public int adminUpd(adminModel adminModel) 
        {
        
          var adminUpds= HotelConText.admins.Find(adminModel.id);
            if (adminUpds!=null)
            {
                adminModel.UserName = adminUpds.UserName;
                adminModel.UserPwd = adminUpds.UserPwd;

            }

            return HotelConText.SaveChanges();
        }





    }
}